import {Injectable} from '@angular/core';
import {Headers, Http} from '@angular/http';
import {ParamConfig} from '../common/param.config';
import 'rxjs/add/operator/toPromise';
import {GetEventInfo} from './getEvent';
import {AddEventService} from '../event/event.service';
import {UserService} from '../user/user.service';
import {Router} from '@angular/router';

@Injectable()
export class GetEventService {
  public token: string;
  public id: string;
  public fileId: string;
  public type: string;
  public mapId: string;
  private hostUrl = ParamConfig.HTTP_WEB_HOST_URL + 'event/getEventById'; // 事件id
  private hostUrlUpdateLogic = ParamConfig.HTTP_WEB_HOST_URL + 'event/updateEvent'; // 修改
  private hostUrlDeleteLogic = ParamConfig.HTTP_WEB_HOST_URL + 'rbFile/deleteByfileId'; // 删除图片
  constructor(private http: Http, private addEventService: AddEventService, private userService: UserService, private router: Router) {
  }
  // 根据id查询当前修改信息
  getEventIdInfo(): Promise<any> {
    this.id = this.addEventService.sendEventId();
    const request = JSON.stringify({
      eventId: this.id
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrl, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            this.type = body.eventTO.type;
            if (body.eventTO.systemFileTO.length !== 0) {
              this.fileId = body.eventTO.systemFileTO[0].fileId;
            }
            if (body.eventTO.systemFileTO.length === 0) {
              this.fileId = '';
            }
            return [true, body.eventTO];
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }

  // 修改逻辑文字
  updateLogicTextinfo(getEventInfo: GetEventInfo, fileId): Promise<any> {
    const request = JSON.stringify({
      eventId: this.id,
      name: getEventInfo.eventName,
      type: this.type,
      sort: getEventInfo.sort,
      eventUrlId: fileId,
      text: getEventInfo.text,
      textId: this.fileId,
      nodeUrlId: fileId
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlUpdateLogic, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            return true;
          }
        }
      )
      .catch(this.handleError);
  }

// 视频
  updateLogicVideoinfo(getEventInfo: GetEventInfo, fileId, fileIdVideo): Promise<any> {
    const request = JSON.stringify({
      eventId: this.id,
      name: getEventInfo.eventName,
      type: this.type,
      sort: getEventInfo.sort,
      text: getEventInfo.text,
      eventUrlId: fileId,
      textId: this.fileId,
      fileIds: fileIdVideo,
      nodeUrlId: fileId
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlUpdateLogic, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            return true;
          };
        }
      )
      .catch(this.handleError);
  }

// 图片
  updateLogicImageinfo(getEventInfo: GetEventInfo, fileId, fileIdImage): Promise<any> {
      const request = JSON.stringify({
      eventId: this.id,
      name: getEventInfo.eventName,
      type: this.type,
      sort: getEventInfo.sort,
      fileIds: fileIdImage,
      text: getEventInfo.text,
      eventUrlId: fileId,
      textId: this.fileId,
      nodeUrlId: fileId
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlUpdateLogic, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            return true;
          };
        }
      )
      .catch(this.handleError);
  }

  // 删除图片
  deleteLogicImageinfo(fileIdImage): Promise<any> {
    const request = JSON.stringify({
      eventId: this.id,
      fileId: fileIdImage,
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlDeleteLogic, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            return true;
          };
        }
      )
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<[boolean, any]> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject([false, error.toString()]);
  }
}
